package net.yunzhanyi.common.web.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * @author qiuxin
 * @code YunzhanyiConfig
 * @date 2022/3/27
 * description：
 */

@ConfigurationProperties(prefix = "yunzhanyi")
@Component
public class YunzhanyiConfig implements ApplicationListener<WebServerInitializedEvent> {

    Logger log = LoggerFactory.getLogger(YunzhanyiConfig.class);

    private String name = "云沾衣";

    private String version = "1.0.0-SNAPSHOT";

    private String moduleName = "通用模块";


    private String resourcePath = "D://yunzhanyi";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getResourcePath() {
        return resourcePath;
    }

    public void setResourcePath(String resourcePath) {
        this.resourcePath = resourcePath;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
        // 获取端口号
        int port = webServerInitializedEvent.getWebServer().getPort();
        // 获取应用名
        String applicationName = webServerInitializedEvent.getApplicationContext().getApplicationName();
        String hostAddress = "";
        try {
            hostAddress = Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        // 打印 swagger 文档地址
        log.info("\n-----------------------------------------" +
                "\n(*^▽^*)  " + name + "  " + version + "  " + moduleName + " 启动成功 " +
                "\n(σﾟ∀ﾟ)σ..:*☆哎哟不错哦" +
                "\n项目地址:" +"\n http://"+hostAddress+":" + port + applicationName+
                "\nswagger 接口文档地址:" +
                "\n http://"+hostAddress+":" + port + applicationName + "/swagger-ui/" +
                "\n-----------------------------------------");
    }


}
